import React from 'react';
import {Table, Row, Col, Form, Input, Icon, InputNumber, Radio, DatePicker, Select, Upload, Button, Switch, Modal, Popconfirm, Tabs, message} from 'antd';
const R = require('ramda');

import EditProduct from '../components/EditProduct';
import RightBox from "../../commons/RightBox";
import RIGHT from "../../../utils/RightConstants";

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const Option = Select.Option;
const TabPane = Tabs.TabPane;

export default Form.create({wrappedComponentRef: true})(class Product extends React.Component {
    constructor(props) {
        super(props);
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource
        this.state = {

        };
    }

    collectSearchCondition = () => {
        let conditions = this.props.form.getFieldsValue();
        return conditions;
    }

    handleResetFields = () => {
        this.props.form.resetFields();
    }

    handleOpenAdd = () => {
        this.editProduct.handleOpen("add");
    }


    handleCopy = (record) => {
        this.props.dispatch({
            type: 'orderManage/copyProudctById',
            payload: {
                id: record.id,
                searchConditions: this.collectSearchCondition()
            }
        });
    }

    onEdit = (record, type) => {
        this.props.dispatch({type: 'customerManage/getProductById', payload: {id: record.id}});
        this.editProduct.handleOpen(type);
    };

    handleSearch = () => {
        let values = this.props.form.getFieldsValue();
        let newPage = this.props.productPage;
        newPage.page = 1;
        this.props.dispatch({
            type: 'customerManage/getProductList',
            payload: {
                page: newPage,
                cstId: this.props.cstid,
                productName: values.productName,
                productModel: values.productModel,
                status: this.props.businessType === "2" ? "4" :values.status,
            }
        });
    };

    columns = [
        {title: '品名', dataIndex: 'name', key: 'name'},
        {title: '品牌', dataIndex: 'branch', key: 'branch'},
        {title: '品牌类型',dataIndex: 'branchtypename',key: 'branchtypename'},
        {title: '型号', dataIndex: 'model', key: 'model'},
        {title: '价格', dataIndex: 'price', key: 'price'},
        {title: '净重(kg)', dataIndex: 'weight', key: 'weight'},
        {title: '申请时间', dataIndex: 'createtime', key: 'createtime'},
        {title: '退税率(%)', dataIndex: 'taxrate', key: 'taxrate'},
        {title: '申报要素', dataIndex: 'declarelements', key: 'declarelements'},
        {title: '操作', key: 'key_oprator',
            render: (text, record, index) => {
                return (
                    <span>
                        {this.props.disabled == true || record.status == 2 || record.status == 4 || record.status == 5 ?
                            <span>
                                {record.status == 4 ?
                                    <span>
                                 <a href="#" onClick={this.handleCopy.bind(this, record, "view")}>复制</a>
                                 <span className="ant-divider"/>
                                    </span>
                                    :""}
                                <a href="#" onClick={this.onEdit.bind(this, record, "view")}>查看</a>
                            </span>
                            :
                            <span>
                                {record.status == 1 || record.status == 3 ?
                                    <span>
                                        <RightBox resource={RIGHT.SUPPLIER.SUPPLIER_DETAIL_PRODUCT_EDIT}><a href="#" disabled={this.props.disabled}
                                           onClick={this.onEdit.bind(this, record, "edit")}>编辑</a>
                                        <span className="ant-divider"/></RightBox>
                                         <RightBox resource={RIGHT.SUPPLIER.SUPPLIER_DETAIL_PRODUCT_DELETE}><a href="#" disabled={this.props.disabled} onClick={
                                             () => {
                                                 this.props.dispatch({
                                                     type: 'customerManage/deleteProductById',
                                                     payload: {
                                                         id: record.id,
                                                         cstid: record.cstid,
                                                         onSuccess:()=>{
                                                             this.handleSearch();
                                                         }
                                                     }
                                                 })
                                             }
                                         }>删除</a></RightBox>
                                    </span>
                                    : ""}

                            </span>
                        }
                    </span>
                );
            }
        }];

    render() {
        const { getFieldDecorator } = this.props.form;
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        let formItemLayout = {
            labelCol: { xs: { span: 24 }, sm: { span: 6 } },
            wrapperCol: { xs: { span: 24 }, sm: { span: 14 } }
        };
        let values = this.props.form.getFieldsValue();
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.productPage.size,
            total: this.props.productPage.totalRow,
            showSizeChanger: true,
            current: this.props.productPage.page,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.productPage;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'customerManage/getProductList',
                    payload: {
                        page: newPage,
                        cstId: this.props.cstid,
                        productName: values.productName,
                        status: this.props.businessType === "2" ? "4" :values.status
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.productPage;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'customerManage/getProductList',
                    payload: {
                        page: newPage,
                        cstId: this.props.cstid,
                        productName: values.productName,
                        status: this.props.businessType === "2" ? "4" :values.status
                    }
                });
            }
        }

        return (
            <div>
                <Form>
                    <Row>
                        <Col span={7}><FormItem {...formItemLayout} label={'名称'}>{getFieldDecorator('productName')(<Input/>)}</FormItem></Col>
                        <Col span={7}><FormItem {...formItemLayout} label={'型号'}>{getFieldDecorator('productModel')(<Input/>)}</FormItem></Col>
                        <Col span={3} style={{float: 'left'}}>
                            <Button type="primary" icon="search" style={{marginRight: 5}} onClick={this.handleSearch.bind(this)}>查询</Button>
                        </Col>
                        <RightBox resource={RIGHT.SUPPLIER.SUPPLIER_DETAIL_PRODUCT_ADD}><Col span={3} style={{float: 'left'}}>
                            <Button type="primary" icon="plus" size={'default'} disabled={this.props.disabled} onClick={this.handleOpenAdd.bind(this)} style={{marginLeft:5}}>增加产品</Button>
                        </Col></RightBox>

                    </Row>
                    {this.props.businessType === "2" ? ""
                        :
                        <Row>
                            <Col span={7}>
                                <FormItem {...formItemLayout} label={'状态'}>
                                    {getFieldDecorator('status', {
                                        initialValue: "1"
                                    })(
                                        <RadioGroup style={{width: 800}}>
                                            <Radio value="1">待申请</Radio>
                                            <Radio value="2">待审核</Radio>
                                            <Radio value="3">待补充资料</Radio>
                                            <Radio value="4">审核通过</Radio>
                                            <Radio value="5">审核不通过</Radio>
                                        </RadioGroup>
                                    )}
                                </FormItem>
                            </Col>

                        </Row>
                    }
                </Form>
                <Table dataSource={this.props.productList} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} scroll={{x:true}}/>

                <EditProduct wrappedComponentRef={(inst) => this.editProduct = inst} dispatch={this.props.dispatch} dataSource={this.props.product} cstId={this.props.cstid}
                             handleSearch= {this.handleSearch} businessType={this.props.businessType} isCstPoduct={true}/>

            </div>
        )
    }
})


// WEBPACK FOOTER //
// ./public/src/components/supplier/components/Product.js